<?php
namespace Home\Controller;
class CreditController extends HomeBaseController
{
    /**
     * 查询借款信息
     * 
     */
    public function loaderInfo(){
        if(I('tim_type','')!=''){
            $condi['a.tim_type']=I('tim_type','');
        }
        $this->assign('tim_type',I('tim_type',''));
        
        $domtypeslec=I('domtypeslec','');
        if($domtypeslec!=''){
            $condi['a.domaintypeid']=$domtypeslec;
        }
        $this->assign('domtypeslec',$domtypeslec);
        
        $domTypes=M('DomainType')->field('id,name')->select();
        $this->assign('domTypes',$domTypes);
        
        if(I('rate','')!=''){
            $rate = I('rate','');
            if($rate==1){
                $condi['a.rate'] = array('elt',9);
                //array(array('egt',date("Y-m-d",strtotime("-6 day")).' 00:00:00'),array('elt',date("Y-m-d",strtotime("2 day")).' 00:00:00'));
            }else if($rate==2){
                $condi['a.rate']=array(array('egt',9),array('elt',10.2));
            }else if($rate==3){
                $condi['a.rate']=array(array('egt',10.2),array('elt',11.4));
            }else if($rate==4){
                $condi['a.rate']=array('egt',11.4);
            }
        }
        $this->assign('rate',I('rate',''));
        $term = I('term','');
        $this->assign('term',I('term',''));
        if($term!='1' && $term!=''){
            if($term==2){//短期
                $condi['a.term']=array(array('egt',1),array('lt',30));
            }else if($term==3){
                $condi['a.term']=30;
            }else if($term==4){
                $condi['a.term']=60;
            }else if($term==5){
                $condi['a.term']=array('egt',60);
            }
        }
        $condi['a.status']= array('in','1,2');
        $condi['a.is_open']=0;
        $money = I('money','');//投资金额
        $this->assign('money',I('money',''));
        if($money!=''){
            if($money==1){
                $condi['a.money']=array('elt',50000);
            }else if($money==2){
                $condi['a.money']=array(array('egt',50000),array('elt',100000));
            }else if($money==3){
                $condi['a.money']=array(array('egt',100000),array('elt',150000));
            }else if($money==4){
                $condi['a.money']=array('egt',150000);
            }
        }
        $allInvemoney = M('LoaderInfo')->where('status=2')->sum('money');
        $allInvemoney= ($allInvemoney!=null)?$allInvemoney:0;
        $allInvecount = M('LoaderInfo')->where('status=2')->count();
        $allInvecount= ($allInvecount!=null)?$allInvecount:0;
        $allRate = M('CredFlow')->sum('rate*rpersionum');
        $allRate= ($allRate!=null)?$allRate:0;
        $this->assign('allInvemoney',$allInvemoney);
        $this->assign('allInvecount',$allInvecount);
        $this->assign('allRate',$allRate);
        $loaderInfolist = M('LoaderInfo')->field('a.*,b.name')->join("as a left join dd_domain_type b on a.domaintypeid=b.id")->where($condi)->select();
        $this->assign('investlist',$loaderInfolist);
        $this->display('credit/loaderInfo');
    }
    /*
     * 查询投资信息
     */
    public function investorinfo(){
        if(I('rate','')!=''){
            $rate = I('rate','');
            if($rate==1){
                $condi['dd_inves_domcate.rate'] = array('elt',9);
                //array(array('egt',date("Y-m-d",strtotime("-6 day")).' 00:00:00'),array('elt',date("Y-m-d",strtotime("2 day")).' 00:00:00'));
            }else if($rate==2){
                $condi['dd_inves_domcate.rate']=array(array('egt',9),array('elt',10.2));
            }else if($rate==3){
                $condi['dd_inves_domcate.rate']=array(array('egt',10.2),array('elt',11.4));
            }else if($rate==4){
                $condi['dd_inves_domcate.rate']=array('egt',11.4);
            }
        }
        $this->assign('rate',I('rate',''));
        $term = I('term','');
        $this->assign('term',I('term',''));
        if($term!='0'){
            if($term=='1'){
                $condi['dd_investor_info.term_begin']=array('egt',1);
                $condi['dd_investor_info.term_end']=array('elt',30);
            }else if($term=='2'){
                $condi['dd_investor_info.term_begin']=array('egt',30);
                $condi['dd_investor_info.term_end']=array('elt',60);
            }else if($term=='3'){
                $condi['dd_investor_info.term_begin']=array('egt',60);
            }
        }
        
        $domtypeid = I('domtypeid','');//投资金额
        if($domtypeid!=''){
            $condi['dd_inves_domcate.domtypeid']=$domtypeid;
        }
        $condi['dd_inves_domcate.is_open']=0;
        $this->assign('domtypeid',$domtypeid);
        $money = I('money','');//投资金额
        $this->assign('money',I('money',''));
        if($money!=''){
            if($money==1){
                $condi['dd_user_assets.creditmoney']=array(array('gt',100),array('elt',50000));
            }else if($money==2){
                $condi['dd_user_assets.creditmoney']=array(array('egt',50000),array('elt',100000));
            }else if($money==3){
                $condi['dd_user_assets.creditmoney']=array(array('egt',100000),array('elt',150000));
            }else if($money==4){
                $condi['dd_user_assets.creditmoney']=array('egt',150000);
            }
        }else{
            $condi['dd_user_assets.creditmoney']=array('gt',100);
        }
        $allInvemoney = M('UserAssets')->sum('creditmoney');
        $allInvemoney= ($allInvemoney!=null)?$allInvemoney:0;
        $allInvecount = M('CredFlow')->count();
        $allInvecount= ($allInvecount!=null)?$allInvecount:0;
        $allRate = M('CredFlow')->sum('money');
        $allRate= ($allRate!=null)?$allRate:0;
        $this->assign('allInvemoney',$allInvemoney);
        $this->assign('allInvecount',$allInvecount);
        $this->assign('allRate',$allRate);
       $InvestorInfodao= M('InvestorInfo');
       
       $investlist = $InvestorInfodao
        ->join(' LEFT JOIN dd_user_assets on dd_investor_info.uid=dd_user_assets.uid')
        ->join(' LEFT JOIN dd_inves_domcate on dd_investor_info.id=dd_inves_domcate.inve_id')
        ->field('dd_investor_info.id,dd_investor_info.term_begin,dd_investor_info.term_end,SUM(dd_investor_info.tovalue) as tovalue,dd_user_assets.creditmoney as money,dd_inves_domcate.rate,dd_inves_domcate.domtypeid,dd_inves_domcate.domtypname,dd_inves_domcate.credit')
        ->where($condi)->group('dd_investor_info.id,dd_inves_domcate.domtypeid')->order("dd_investor_info.create_time desc")->select();
        
        $this->assign('investlist',$investlist);
        $domTypes=M('DomainType')->field('id,name')->select();
        $this->assign('domTypes',$domTypes);
        $this->display('credit/investorinfo');
    }
    
    public function loader(){
        if(session('uid')==null){
            $this->ajaxReturn(array('code'=>'error','info' => '请选去登录！'));//0错误，1成功
            exit();
        }
        $cond['a.uid']=session('uid');
        $cond['a.check_status']=3;
        $cond['a.trans_status']=1;
        $cond['a.is_mortage']=1;
        
        $domainDao=M('UserDomains');
        $domlist=$domainDao->join('as a LEFT JOIN dd_domain_type b ON a.domain_typeid=b.id')->
        field('a.id,a.domain_typeid,a.domain,b.name')->where($cond)->select();
        $count=$domainDao->join('as a LEFT JOIN dd_domain_type b ON a.domain_typeid=b.id')->
        where($cond)->count();
        $this->assign('count',$count);
        $this->assign('domlist',$domlist);
        $this->display('credit/loader');
    }
        
    public function investor(){
    
        if(session('uid')==null){
            $this->ajaxReturn(array('code'=>'error','info' => '请选去登录！'));//0错误，1成功
            exit();
        }
    
        $creditmoney=M('UserAssets')->field('creditmoney')->where('uid='.session('uid'))->select();
        $this->assign('creditmoney',$creditmoney[0]['creditmoney']);
        $domainTypelist = M('DomainType')->field('id,name')->order('id')->select();
        $this->assign('domainTypelist',$domainTypelist);
        $this->display('credit/investor');
    }
    
    
    public function loandetail(){
        if(session('uid')==null){
            $this->display("login/index");
            exit();
        }
        $id=I('id','');
        $info = M('LoaderInfo')->find($id);
        $user = M('User')->field('username')->where('id='.$info['uid'])->select();
        $this->assign('username',$user[0]['username']);
        $this->assign('info',$info);
        $this->assign('shengyu',$info['money']-$info['tovalue']);
        $catename=M('DomainType')->field('name')->find($info['domaintypeid']);
        $this->assign('catename',$catename['name']);
                    /*  月利率=年利率/12
       日利率=月利率/30=年利率/360 */
        $creditmoney=M('UserAssets')->field('creditmoney')->where('uid='.session('uid'))->select();
        $this->assign('creditmoney',$creditmoney[0]['creditmoney']);
        
        $this->display("credit/loaderdetail");
    }
    
    
    public function inverdetail(){
        if(session('uid')==null){
            $this->display("login/index");
            exit();
        }
        $id=I('id','');
        $info = M('InvestorInfo')->find($id);
        $user = M('User')->field('username')->where('id='.$info['uid'])->select();
        $this->assign('username',$user[0]['username']);
        $this->assign('info',$info);
        $creditmoney=M('UserAssets')->field('creditmoney')->where('uid='.$info['uid'])->select();
        $this->assign('shengyu',$creditmoney[0]['creditmoney']);
        $invecate=M('InvesDomcate')->field('credit,rate,domtypname')->where('inve_id='.$info['id'].' and domtypeid='.I('cateid',''))->select();
        $this->assign('invecate',$invecate[0]);
        $tradeFee = M('TradeFee')->field('rate')->where('type=3')->select();
        //计算若贷款1个月需要支付多少利息
        $lixi=$invecate[0]['rate']*0.01;
        $lixi=($invecate[0]['credit']*$lixi/12);
        $this->assign('lixi',$lixi);
        $this->assign('tradeFee',($tradeFee[0]['rate'])*0.01);
        $this->assign('jindu',sprintf("%1.2f",($info['tovalue']/$creditmoney[0]['creditmoney'])*100));
        $this->assign('cateid',I('cateid',''));
        $this->display("credit/inverdetail");
    }
    
}